package com.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.erp.common.BaseContext;
import com.erp.common.SuperConstant;
import com.erp.dao.GeneralLedgerDao;
import com.erp.entity.FinancialJournal;
import com.erp.entity.GeneralLedger;
import com.erp.service.FinancialJournalService;
import com.erp.service.GeneralLedgerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Service
public class GeneralLedgerServiceImpl extends ServiceImpl<GeneralLedgerDao, GeneralLedger> implements GeneralLedgerService {

    @Autowired
    private FinancialJournalService financialJournalService;

    @Override
    @Transactional
    public boolean updateById(GeneralLedger generalLedger) {
        FinancialJournal financialJournal = new FinancialJournal
                (BaseContext.getCurrentId(), SuperConstant.GeneralLedgerTable, SuperConstant.Update,generalLedger.getId().toString());
        financialJournalService.save(financialJournal);

        return super.updateById(generalLedger);
    }

    @Override
    @Transactional
    public boolean save(GeneralLedger generalLedger) {
        //类型校验
        LambdaQueryWrapper<GeneralLedger> ledgerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ledgerLambdaQueryWrapper.eq(GeneralLedger::getType,generalLedger.getType());
        List<GeneralLedger> list = this.list(ledgerLambdaQueryWrapper);
        if (list.size()>0){
            return false;
        }

        //记录日志
        FinancialJournal financialJournal = new FinancialJournal
                (BaseContext.getCurrentId(), SuperConstant.GeneralLedgerTable, SuperConstant.Save);
        financialJournalService.save(financialJournal);

        return super.save(generalLedger);
    }

    @Override
    public boolean removeById(Serializable id) {
        //记录日志
        FinancialJournal financialJournal = new FinancialJournal
                (BaseContext.getCurrentId(), SuperConstant.GeneralLedgerTable, SuperConstant.Delete,id.toString());
        financialJournalService.save(financialJournal);

        return super.removeById(id);
    }

}
